package com.ruoyi.packag.controller;

import cn.jy.common.index.Count;
import cn.jy.common.index.LineChart;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.packag.config.AliyunSmsUtil;
import com.ruoyi.packag.domain.Message;
import com.ruoyi.packag.domain.Package;
import com.ruoyi.packag.service.PackageService;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("package")
@CrossOrigin
public class PackageController {
    @Autowired
    private PackageService packageService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliyunSmsUtil aliyunSmsUtil;

    @RequestMapping("show")
    public PageInfo<Package> show(@RequestBody Package p) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<Package> show = packageService.show(p);
        PageInfo<Package> packagePageInfo = new PageInfo<>(show);
        return packagePageInfo;
    }

    @RequestMapping("cuiqu")
    public int showExpressAccount(@RequestBody Package p) {
        String photo = p.getExpressList().getPhone();
        aliyunSmsUtil.sendSms(photo, "111111");
        Message message = new Message();
        message.setDeliveryid(Math.toIntExact(p.getExpressList().getId()));
        message.setTitle("快点送快递");
        message.setContent("你还有快递没有送");
        packageService.add(message);
        return 200;
    }

    @RequestMapping("chongfa")
    public int showExpressList(@RequestBody Package p) {
        Random random = new Random();
        int randomNumber = 100000 + random.nextInt(900000);
        String photo = p.getExpressList().getPhone();
        aliyunSmsUtil.sendSms(photo, String.valueOf(randomNumber));
        p.getExpressList().setExpresscode(randomNumber);
        p.setQujianma(randomNumber);
        packageService.chong(p);
        Message message = new Message();
        message.setDeliveryid(Math.toIntExact(p.getExpressList().getId()));
        message.setTitle("取件码已更改");
        message.setContent("新的快递吗是"+randomNumber);
        packageService.add(message);
        System.out.println("取件码是"+randomNumber);
        return 200;
    }

    @RequestMapping("deliveryCount")
    public Count deliveryCount() {
        return packageService.deliveryCount();
    }//派件统计

    @RequestMapping("findLineChartDelivery")
    public List<LineChart> findLineChartDelivery() {
        return packageService.findLineChartDelivery();
    }//派件折线图

    @RequestMapping("sendCount")
    public Count sendCount() {
        return packageService.sendCount();
    }//寄件统计

    @RequestMapping("findLineChartSend")
    public List<LineChart> findLineChartSend() {
        return packageService.findLineChartSend();
    }//寄件折线图

}
